package com.wan.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wan.entity.*;
import com.wan.service.*;
import com.wan.service.system.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static java.util.stream.Collectors.toList;

/**
 * 后台管理 home控制层
 * @author xzx
 */
@Slf4j
@Controller
@RequestMapping("/map")
public class HomeController {

    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private OfficeBuildingRoomService roomService;
    @Autowired
    private LivingBuildingService livingBuildingService;
    @Autowired
    private LivingBuildingRoomService buildingRoomService;
    @Autowired
    private GameAreaService collageService;
    @Autowired
    private GameUserService gameUserService;
    @Autowired
    private GameRoleService gameRoleService;
    @Autowired
    private GameHeroService gameHeroService;
    @Autowired
    private GameRecordService recordService;
    @Autowired
    private GameAnnouncementService announcementService;






    @RequestMapping("/test")
    public String tsetSession(Model model) {
        model.addAttribute("sessionTimeout","登录超时,请重新登录");
        return "redirect:/user/login";
    }

    /**
     * 跳转首页
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView gotoIndex(){
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }

    @RequestMapping("/gonggao/page")
    public ModelAndView gongGaoPage(){
        ModelAndView modelAndView = new ModelAndView("newsAdd");
        return modelAndView;
    }

    @RequestMapping("/upload/office/page")
    public ModelAndView gotoUploadTable(){
        ModelAndView modelAndView = new ModelAndView("uploadoffice");
        List<GameRole> roleList = gameRoleService.list();
        List<GameHero> gameHeroes = gameHeroService.list();
        String gameStartTime = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        modelAndView.addObject("roleList",roleList);
        modelAndView.addObject("gameHeroes",gameHeroes);
        modelAndView.addObject("gameStartTime",gameStartTime);
        return modelAndView;
    }


    @RequestMapping("/upload/living/page")
    public ModelAndView gotoUploadLiving(){
        ModelAndView modelAndView = new ModelAndView("uploadliving");
        List<GameArea> collageInfos = collageService.list();
        modelAndView.addObject("list",collageInfos);
        return modelAndView;
    }

    @RequestMapping("/upload/office/room/page")
    public ModelAndView gotoUploadRoomTable(){
        ModelAndView modelAndView = new ModelAndView("uploadofficeroom");
        List<GameArea> officeBuilds = collageService.list();
        modelAndView.addObject("list",officeBuilds);
        return modelAndView;
    }


    @RequestMapping("/upload/living/room/page")
    public ModelAndView gotoUploadLiveRoomTable(){
        ModelAndView modelAndView = new ModelAndView("uploadlivingroom");
        List<GameArea> areas = collageService.list();
        List<GameUser> userList = gameUserService.list();
        modelAndView.addObject("list",areas);
        modelAndView.addObject("userList",userList);
        return modelAndView;
    }



    @RequestMapping("/edit/livingbuild/page/{livingBuildId}")
    public ModelAndView gotoEditLiving(@PathVariable("livingBuildId") String livingBuildId){
        ModelAndView modelAndView = new ModelAndView("editlivingbuild");
        GameUser gameUser = gameUserService.getById(livingBuildId);
        List<GameArea> collageInfos = collageService.list();
        modelAndView.addObject("list",collageInfos);
        modelAndView.addObject("obj",gameUser);
        return modelAndView;
    }


    @RequestMapping("/edit/gonggao/page/{livingBuildId}")
    public ModelAndView gotoEditGonggao(@PathVariable("livingBuildId") String livingBuildId){
        ModelAndView modelAndView = new ModelAndView("editgonggao");
        GameAnnouncement gameAnnouncement = announcementService.getById(livingBuildId);
//        List<GameAnnouncement> collageInfos = collageService.list();
//        modelAndView.addObject("list",collageInfos);
        modelAndView.addObject("obj",gameAnnouncement);
        return modelAndView;
    }

    @RequestMapping("/edit/liveroom/page/{livingRoomId}")
    public ModelAndView gotoEditLiveRoomTable(@PathVariable("livingRoomId") String livingRoomId){
        ModelAndView modelAndView = new ModelAndView("editlivingroombuild");
        LivingRoomInfos roomInfos = buildingRoomService.getById(livingRoomId);
        List<LivingBuilding> livingBuildings = livingBuildingService.list();
        modelAndView.addObject("list",livingBuildings);
        modelAndView.addObject("obj",roomInfos);
        return modelAndView;
    }



    @RequestMapping("/edit/office/page/{officeId}")
    public ModelAndView gotoEditTable(@PathVariable("officeId") String officeId){
        ModelAndView modelAndView = new ModelAndView("editoffice");
        OfficeBuild officeBuild = officeBuildingService.getById(officeId);
        List<GameArea> collageInfos = collageService.list();
        modelAndView.addObject("list",collageInfos);
        modelAndView.addObject("obj",officeBuild);
        return modelAndView;
    }



    @RequestMapping("/edit/officeroom/page/{roomId}")
    public ModelAndView gotoEditRoomTable(@PathVariable("roomId") String roomId){
        ModelAndView modelAndView = new ModelAndView("editofficeroom");
        OfficeRoomInfos roomInfos = roomService.getById(roomId);
        List<OfficeBuild> officeBuilds = officeBuildingService.list();
        modelAndView.addObject("list",officeBuilds);
        modelAndView.addObject("obj",roomInfos);
        return modelAndView;
    }



    public static List<Map<Object, Object>> compareListHitData(List<Map<Object, Object>> oneList, List<Map<Object, Object>> twoList) {
        List<Map<Object, Object>> resultList = oneList.stream().map(map -> twoList.stream()
                .filter(m -> Objects.equals(m.get("id"), map.get("id")))
                .findFirst().map(m -> {
                    map.putAll(m);
                    return map;
                }).orElse(null))
                .filter(Objects::nonNull).collect(toList());
        return resultList;
    }


    @RequestMapping("/officebuild/list")
    public ModelAndView getVegetable() {
        Integer pageSize = 500;
        ModelAndView modelAndView = new ModelAndView("officebuildlist");
        IPage<GameRecord> page = new Page<>(1,pageSize);
        QueryWrapper<GameRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        IPage<GameRecord> officeBuildIPage = recordService.page(page,queryWrapper);
        Long count = officeBuildIPage.getTotal();
        long totalPage  = (count + pageSize -1) / pageSize;
        List<GameRecord> records = officeBuildIPage.getRecords();
        //List<MenuInfo> menuInfos = menuInfoService.list();
        //modelAndView.addObject("meuList",menuInfos);
        modelAndView.addObject("list",records);
        modelAndView.addObject("count",count);
        modelAndView.addObject("totalPage",totalPage);
        return modelAndView;
    }



    @RequestMapping("/gonggao/list")
    public ModelAndView gongList(){
        Integer pageSize = 500;
        ModelAndView modelAndView = new ModelAndView("gonggaolist");
        IPage<GameAnnouncement> page = new Page<>(1,pageSize);
        QueryWrapper<GameAnnouncement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        IPage<GameAnnouncement> officeBuildIPage = announcementService.page(page,queryWrapper);
        Long count = officeBuildIPage.getTotal();
        long totalPage  = (count + pageSize -1) / pageSize;
        List<GameAnnouncement> records = officeBuildIPage.getRecords();
        //List<MenuInfo> menuInfos = menuInfoService.list();
        //modelAndView.addObject("meuList",menuInfos);
        modelAndView.addObject("list",records);
        modelAndView.addObject("count",count);
        modelAndView.addObject("totalPage",totalPage);
        return modelAndView;
    }

    @RequestMapping("/livingbuilding/list")
    public ModelAndView getLivingBuild() {
        Integer pageSize = 10;
        ModelAndView modelAndView = new ModelAndView("livingbuildlist");
        IPage<GameUser> page = new Page<>(1,pageSize);
        QueryWrapper<GameUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        IPage<GameUser> livingBuildingIPage = gameUserService.page(page,queryWrapper);
        Long count = livingBuildingIPage.getTotal();
        long totalPage  = (count + pageSize -1) / pageSize;
        List<GameUser> livingBuildings = livingBuildingIPage.getRecords();
        //List<MenuInfo> menuInfos = menuInfoService.list();
        //modelAndView.addObject("meuList",menuInfos);
        modelAndView.addObject("list",livingBuildings);
        modelAndView.addObject("count",count);
        modelAndView.addObject("totalPage",totalPage);
        return modelAndView;
    }







    @RequestMapping("/batching/list")
    public ModelAndView getBatching() {
        ModelAndView modelAndView = new ModelAndView("batchlist");
        return modelAndView;
    }



    @RequestMapping("/officebuildroom/list")
    public ModelAndView getOfficeRoom() {
        Integer pageSize = 10;
        ModelAndView modelAndView = new ModelAndView("officebuildroomlist");
        IPage<OfficeRoomInfos> page = new Page<>(1,pageSize);
        QueryWrapper<OfficeRoomInfos> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("update_time");
        IPage<OfficeRoomInfos> roomInfosIPage = roomService.page(page,queryWrapper);
        Long count = roomInfosIPage.getTotal();
        long totalPage  = (count + pageSize -1) / pageSize;
        List<OfficeRoomInfos> records = roomInfosIPage.getRecords();
        //List<MenuInfo> menuInfos = menuInfoService.list();
        //modelAndView.addObject("meuList",menuInfos);
        modelAndView.addObject("list",records);
        modelAndView.addObject("count",count);
        modelAndView.addObject("totalPage",totalPage);
        return modelAndView;
    }


    @RequestMapping("/livingroom/list")
    public ModelAndView getLiveRoom() {
        Integer pageSize = 10;
        ModelAndView modelAndView = new ModelAndView("livingbuildroomlist");
        IPage<GameRole> page = new Page<>(1,pageSize);
        QueryWrapper<GameRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        IPage<GameRole> roomInfosIPage = gameRoleService.page(page,queryWrapper);
        Long count = roomInfosIPage.getTotal();
        long totalPage  = (count + pageSize -1) / pageSize;
        List<GameRole> records = roomInfosIPage.getRecords();
        //List<MenuInfo> menuInfos = menuInfoService.list();
        //modelAndView.addObject("meuList",menuInfos);
        modelAndView.addObject("list",records);
        modelAndView.addObject("count",count);
        modelAndView.addObject("totalPage",totalPage);
        return modelAndView;
    }




    @RequestMapping("/upload/batch")
    public ModelAndView gotoUploadBatch(){
        ModelAndView modelAndView = new ModelAndView("uploadbatch");
        return modelAndView;
    }

    @RequestMapping("/main")
    public ModelAndView gotoMain() {
        ModelAndView modelAndView = new ModelAndView("main");
        return modelAndView;
    }

    @RequestMapping("/personInfo")
    public ModelAndView gotoPersonInfo() {
        ModelAndView modelAndView = new ModelAndView("personInfo");
        return modelAndView;
    }

    @RequestMapping("/changepwd")
    public ModelAndView gotoChangePassWord() {
        ModelAndView modelAndView = new ModelAndView("changepwd");
        return modelAndView;
    }

    @RequestMapping("/myloginfo")
    public ModelAndView gotoMyloginfo(){
        ModelAndView modelAndView = new ModelAndView("myloginfo");
        return modelAndView;
    }



}
